import { Router2 } from "../addons/router";
import { withAuth } from "../services/sercurity";

const router = Router2({ base: "/resources" });
export default { base: "/resources/*", handle: router.handle };

router.get("/:url", withAuth, async (request) => {
  const url = request.params.url;
  if (url !== undefined) {
    try {
      const response = await fetch(decodeURI(atob(url)), {
        method: request.method,
        headers: request.headers,
        body: request.body,
      });
      return new Response(response.body, {
        status: response.status,
        statusText: response.statusText,
        headers: Array.from(response.headers),
      });
    } catch (error) {}
  }
  throw new Response("上游服务未及时响应", { status: 504 });
});
